package forum.service.rpc;

import forum.domain.Token;
import forum.repository.TokenRepository;
import forum.service.rpc.api.TokenService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Random;

/**
 * Created by Administrator on 2017/4/19.
 */
@org.springframework.stereotype.Service("tokenService")
public class TokenServiceImpl implements TokenService {

    @Autowired
    private TokenRepository tokenRepository;

    @Override
    public Object getTokenById(Integer id) {
        return null;
    }

    @Override
    public Object createToken(Token token) {
        if (token != null) {
            tokenRepository.save(token);
        }else {
            return "token 生成有误";
        }
        return token;
    }

    public Object findByTokenname(String tokenname) {
        if (tokenname != null) {
            return  tokenRepository.findByTokenname(tokenname);
        }else {
            return "tokenname 是空的";
        }
//         token;
    }



    @Override
    public Object updateUserById(Token token) {
        return null;
    }


    @Override
    public Object deleteTokenByTokenname(String tokenname) {
        tokenRepository.deleteByTokenname(tokenname);
        return "success";
    }



    @Override
    public Object deleteTokenByIds(Integer tokenId) {
        tokenRepository.delete(tokenId);
        return "success";
    }

    int CODE_LENGTH=12;
    /**
     * 所有产生验证码的字符
     */
    private static final char[] CHARS = {
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
//            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
//            'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
//            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
//            'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
    };

    /**
     * 随机得到验证码要显示的字符,个数为我们自己设置
     * @return 得到一个字符串
     */

//    @RequestMapping("/testemail1")
    public String createCode() {
        Random random = new Random();
        StringBuilder buffer = new StringBuilder();
        //根据传入验证码的长度来随机生成数组里面的字符
        for (int i = 0; i < CODE_LENGTH; i++) {
            buffer.append(CHARS[random.nextInt(CHARS.length)]);
        }
        System.out.print(buffer.toString()+"--------------");
        return buffer.toString();
    }



}
